home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 3479 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: news.sni.de!news
  2. From: Josef Moellers <mollers.pad@sni.de>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: strings
  5. Date: 29 Jan 1996 11:07:47 GMT
  6. Organization: Siemens Nixdorf Informationssysteme AG, Paderborn, Germany
  7. Message-ID: <4ei9q3$fh1@nervous.pdb.sni.de>
  8. References: <4eg9qj$1ut4@sp115.ocs.lsu.edu> <TANMOY.96Jan28104305@qcd.lanl.gov>
  9. NNTP-Posting-Host: uranium.pdb.sni.de
  10. X-Newsreader: NN version 6.5.0 #2
  11.  
  12. In <TANMOY.96Jan28104305@qcd.lanl.gov> tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya) writes:
  13.  
  14. >In article <4eg9qj$1ut4@sp115.ocs.lsu.edu> eenaya@unix1.sncc.lsu.edu
  15. >(Pradeep Nayar) writes: 
  16. ><snip>
  17. >   I saw this piece of code in a book somewhere.  
  18.  
  19. >Throw away the book: immediately.
  20.  
  21. >   char *str ;
  22.  
  23. >   str = "hello world"[10] ;
  24.  
  25. >   what does the second line do?
  26.  
  27. >Nothing meaningful. In fact, the compiler must tell you that this is
  28. >incorrect: otherwise put your compiler exactly in the same place where
  29. >your book deserves to go :-)
  30.  
  31. You _could_ be a little more precise:
  32. Assuming this is C (of which I know something) and not C++ (of which I
  33. know next to nothing), the only problem I see with the two lines quoted
  34. above is the asterisk in the definition of "str". If you leave out the
  35. asterisk, it's the declaration of a character and the assignment of the
  36. letter 'd' to the character.
  37.  
  38. According to K&R/2nd, "A strings has type "array of characters" ...".
  39. So, the second line just uses index 10 to this array and retrieves the
  40. 'd'!
  41.  
  42. Sortlike constructs can be found when converting digits to their
  43. respective ASCII representations:
  44.  
  45.     dig = number % 16;
  46.     cdig = "0123456789abcdef"[dig];
  47.  
  48. Hope this helps.
  49.  
  50. Keep on converting,
  51.  
  52. Josef
  53. --
  54. Josef Moellers        work: mollers.pad@sni.de
  55.             home: josef@firefox.pb.owl.de
  56.